using System;
using System.Collections.Generic;

namespace monoeegcore
{
	public class UnitConverter
	{
		String m_source = "m";
		String m_dest = "m";
		
		static Dictionary<String,double> s_scaling = new Dictionary<String, double>() {{"µm", 0.000001},
			{"mm",0.001}, 
			{"cm",0.01}, 
			{"dm", 0.1},
			{"m", 1.0},
			{"km", 1000.0},
			{"mi", 1609.34},
			{"yd", 0.914400},
			{"ft",0.304800},
			{"in", 0.0254000}};
		
		public UnitConverter (String sourceUnits, String destUnits)
		{
			m_source = sourceUnits;
			m_dest = destUnits;
		}
		
		public double Scaling
		{
			get
			{
				return CalculateScaling(m_source, m_dest);
			}
		}
		
		public String SourceUnits
		{
			get{
				return m_source;
			}
			set
			{
				if(IsValid(value))
				{
					m_source = value;
				}
				
			}
		}
		
		public String DestUnits
		{
			get
			{
				return m_dest;
			}
			set
			{
				if(IsValid(value))
				{
					m_dest = value;
				}
			}
		}
		
		public static double CalculateScaling(String sourceUnits, String destUnits)
		{
			double scaling = 1.0;
			
			double mid = 1.0;
			
			if(s_scaling.ContainsKey(sourceUnits))
			{
				mid = s_scaling[sourceUnits];
			}
			else
			{
				throw new Exception("Scaling factor unknown");
			}
			
			if(destUnits == "m")
			{
				scaling = mid;
			}
			else
			{
				scaling = mid / CalculateScaling(destUnits, "m");
			}
			
			return scaling;
		}
		
		public static bool IsValid(String unit)
		{
			return s_scaling.ContainsKey(unit);
		}
	}
}

